package com.example.lenovo.a203_yhy_ordering_3.model;

import com.example.lenovo.a203_yhy_ordering_3.beans.LoginBean;
import com.example.lenovo.a203_yhy_ordering_3.listener.LoginListener;
import com.example.lenovo.a203_yhy_ordering_3.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by lenovo on 2018/11/12.
 */

public class LoginModel {

    private Retrofit retrofit;

    public LoginModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getResultList(String username, String userpass, final LoginListener loginlistener){

        LoginService loginService = retrofit.create(LoginService.class);

        Call<LoginBean> loginBeanCall = loginService.userLogin(username,userpass);

        loginBeanCall.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response!=null)
                    loginlistener.onResponse(response.body());
                else loginlistener.onFail("获取登录信息失败");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginlistener.onFail(t.toString());
            }
        });
    }


}
